package ldh.im.fxbase.service;

import ldh.im.fxbase.rpc.router.SignalConsumerRouter;
import ldh.im.fxbase.rpc.service.RpcAvService;
import ldh.im.fxbase.rpc.service.RpcSignalService;
import ldh.im.rpc.LoginFilter;
import ldh.im.rpc.LoginSuccessFilter;
import ldh.rpc.RpcConfig;
import ldh.rpc.cache.LocalRpcCacheService;
import ldh.rpc.client.RpcClientBootstrap;
import ldh.rpc.client.RpcClientSocketBootstrap;
import ldh.rpc.client.RpcClientWebSocketBootstrap;
import ldh.rpc.listener.ChannelListener;

public class ChannelService {

    public RpcClientBootstrap createAvClientBootstrap(String address, int port, ChannelListener channelListener) throws Exception {
        RpcConfig rpcConfig = new RpcConfig(new LocalRpcCacheService("client-av"), address, port, "binary");
//        rpcConfig.setCommunicateService(new ImCommunicateService(rpcConfig));

        RpcClientBootstrap clientBootstrap = new RpcClientSocketBootstrap(rpcConfig);
        clientBootstrap.addRouters(RpcAvService.class);
        clientBootstrap.addFilters(new LoginSuccessFilter(clientBootstrap.getRpcConfig()), new LoginFilter());
        clientBootstrap.start(channelListener);
        return clientBootstrap;
    }

    /**
     * websocket 连接信令服务器
     * @param address
     * @param port
     * @return
     * @throws Exception
     */
    public RpcClientBootstrap createWebRtcClientBootstrap(String address, int port, ChannelListener channelListener) throws Exception {
        RpcConfig rpcConfig = new RpcConfig(new LocalRpcCacheService("client-webrtc"), address, port, "json");

        RpcClientBootstrap clientBootstrap = new RpcClientWebSocketBootstrap(rpcConfig, "signal");
        clientBootstrap.addRouters(RpcSignalService.class);
        clientBootstrap.addRouters(SignalConsumerRouter.class);
        clientBootstrap.addFilters(new LoginSuccessFilter(clientBootstrap.getRpcConfig()), new LoginFilter());
        clientBootstrap.start(channelListener);
        return clientBootstrap;
    }
}
